package org.itboys.mobile.service.mongo.weixin;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayNews;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/7/17
 * Description: 集楼_新闻_小程序_service
 */
@Service
public class SundayWeiXinNewsService extends BaseMongoService<SundayNews> {
    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<SundayNews> getEntityClass() {
        return SundayNews.class;
    }

    /**
     * 小程序获取单个新闻
     *
     * @param id
     * @return
     */
    public SundayNews findOne(Long id) {
        Map<String, Object> param = new HashMap<>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("id", id);

        List<SundayNews> list = super.list(param);
        if (ListUtil.isNotNull(list)){
            return list.get(0);

        }else {
            throw new ErrorException("数据丢失");
        }
    }

    /**
     * 分页查询: 查询出设置为首页推荐的新闻
     *
     * @param request
     * @return
     */
    public List<SundayNews> select(HttpServletRequest request) {
        Map<String, Object> param = new HashMap<>();
        param.put("isRecommend", SundayCommonConstants.YES);
        param.put("sort", "ct");
        param.put("order", "desc");
        param.put("isDeleted", 0);

        List<SundayNews> list = super.pageQuery(request, param).getData();
        if (ListUtil.isNotNull(list)) {
            return list;
        } else {
            throw new ErrorException("没有设置为首页推荐的新闻");
        }
    }

}
